/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package restaurante;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.DepthTest;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ToolBar;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 *
 * @author 66785403
 */
public class Restaurante extends Application {

    private ToolBar toolBar;
    private double mouseDragOffsetX;
    private double mouseDragOffsetY;
    private PanelFiguras shapePane;
    final private StackPane layerPane = new StackPane();
    private ScrollButtons scrollButtons;
    private BorderPane root;
    private Area mesa = null;
    private Salon draw;
    public ScrollPane sp = new ScrollPane();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage primaryStage) {


        root = new BorderPane() {

            @Override
            protected void layoutChildren() {
                super.layoutChildren();
                if (mesa != null) {
                }
            }
        };

        root.setOnMouseMoved(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                if (mesa != null) {
                    double x = t.getSceneX();
                    double y = t.getSceneY();
                    moveObject(mesa, x, y);
                    if (!checkPosition(mesa, x, y)) {
                        mesa.shape.setStroke(Color.rgb(255, 29, 0));
                    } else {
                        mesa.shape.setStroke(Color.rgb(72, 178, 0));
                    }
                }
            }
        });

        root.setOnKeyPressed(new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent t) {

                switch (t.getCode()) {
                    case ESCAPE:
                        root.getChildren().remove(mesa);
                        mesa = null;
                        break;
                    case M:

                }
            }
        });



        layerPane.setDepthTest(DepthTest.DISABLE);
        layerPane.getChildren().add(root);
        Scene scene = new Scene(layerPane, 800, 600);
        scene.getStylesheets().add(Restaurante.class.getResource("salon.css").toExternalForm());
        layerPane.setId("layer-pane");
        primaryStage.initStyle(StageStyle.UNDECORATED);

        root.setId("root");
        primaryStage.setTitle("Restaurante!");
        scrollButtons = new ScrollButtons();
        draw = new Salon(80, 80, scrollButtons, this);
        draw.setId("draw-pane");
        root.setOnMousePressed(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                if (mesa != null) {
                    double x = t.getSceneX();
                    double y = t.getSceneY();
                    if (checkPosition(mesa, x, y)) {
                        mesa.shape.setStroke(Color.rgb(30, 170, 255));
                        Shape shape = mesa.shape;
                        if (shape instanceof Rectangle) {
                            x -= ((Rectangle) shape).getWidth() / 2;
                            y -= ((Rectangle) shape).getHeight() / 2;
                        }
                        if (mesa instanceof AreaEle) {
                            AreaEle ele = (AreaEle) mesa;
                            x -= ele.width / 2;
                            y -= ele.height / 2;
                        }
                        y -= toolBar.getHeight();
                        Bounds bounds = sp.getViewportBounds();
                        double panelWidth = bounds.getWidth();
                        double panelHeight = bounds.getHeight();
                        double xScroll = sp.getHvalue() * (draw.getWidth() - panelWidth);
                        double yScroll = sp.getVvalue() * (draw.getHeight() - panelHeight);
                        // scroll < 0
                        xScroll = (xScroll < 0) ? 0 : xScroll;
                        yScroll = (yScroll < 0) ? 0 : yScroll;
                        x += xScroll;
                        y += yScroll;
                        root.getChildren().remove(mesa);
                        draw.addShape(mesa, x, y);
                        mesa = null;
                    }
                }
            }
        });



        toolBar = new ToolBar();
        toolBar.setId("mainToolBar");

        toolBar.getItems().add(new Button("Prueba"));
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        toolBar.getItems().add(spacer);

        toolBar.setMinHeight(29);
        toolBar.setMaxWidth(Double.MAX_VALUE);
        //Arrastrar la ventana
        final WindowButtons windowButtons = new WindowButtons(primaryStage);

        toolBar.setOnMousePressed(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                mouseDragOffsetX = event.getSceneX();
                mouseDragOffsetY = event.getSceneY();
            }
        });
        toolBar.setOnMouseDragged(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                if (!windowButtons.isMaximized()) {
                    primaryStage.setX(event.getScreenX() - mouseDragOffsetX);
                    primaryStage.setY(event.getScreenY() - mouseDragOffsetY);
                }
            }
        });

        toolBar.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                if (event.getClickCount() == 2) {
                    windowButtons.toogleMaximized();
                }
            }
        });

        toolBar.getItems().add(windowButtons);

        //Shape Pane
        shapePane = new PanelFiguras(draw, root, this);

        sp.setContent(draw);
        sp.setId("scroll");
        sp.setPannable(true);

        root.setCenter(sp);
        root.setTop(toolBar);

        root.setRight(shapePane);

//        for (int i = 0; i < 5; i++) {
//            draw.addShape(new Rectangle(50, 50), new Random().nextInt(800) + 100, new Random().nextInt(800) + 100);
//        }


        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void setMesa(Area mesa, double x, double y) {
        this.mesa = mesa;
        root.getChildren().add(mesa);
        moveObject(mesa, x, y);
        if (!checkPosition(mesa, x, y)) {
            mesa.shape.setStroke(Color.rgb(255, 29, 0));
        } else {
            mesa.shape.setStroke(Color.rgb(72, 178, 0));
        }
    }

    private void moveObject(Area mesa, double x, double y) {
        Shape shape = mesa.shape;
        if (shape instanceof Circle) {
            mesa.setLayoutX(x);
            mesa.setLayoutY(y);
        } else if (shape instanceof Rectangle) {
            mesa.setLayoutX(x - ((Rectangle) shape).getWidth() / 2);
            mesa.setLayoutY(y - ((Rectangle) shape).getHeight() / 2);
        } else if (mesa instanceof AreaEle) {
            AreaEle ele = (AreaEle) mesa;
            ele.setLayoutX(x - ele.width / 2);
            ele.setLayoutY(y - ele.height / 2);
        }
    }

    private boolean checkPosition(Area mesa, double x, double y) {
        Shape shape = mesa.shape;
        double width;
        double height;
        if (shape instanceof Circle) {
            double radius = ((Circle) shape).getRadius();
            width = radius * 2;
            height = width;
            x -= radius;
            y -= radius;
        } else if (shape instanceof Rectangle) {
            width = ((Rectangle) shape).getWidth();
            height = ((Rectangle) shape).getHeight();
            x -= width / 2;
            y -= height / 2;
        } else if (mesa instanceof AreaEle) {
            AreaEle ele = (AreaEle) mesa;
            width = ele.width;
            height = ele.height;
            x -= width / 2;
            y -= height / 2;
        } else {
            throw new UnsupportedOperationException("No shape found");
        }
        Bounds bounds = sp.getViewportBounds();
        double panelWidth = bounds.getWidth();
        double panelHeight = root.getHeight();
        if (x < bounds.getMinX() || x + width > panelWidth || y < toolBar.getHeight() || y + height > panelHeight) {
            return false;
        }
        return true;
    }
}
